package com.baseframe.sys.service.impl;


import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.baseframe.sys.entity.FrameModulePrivilege;
import com.baseframe.sys.service.dao.FrameModulePrivilegeRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baseframe.common.utils.Constant;
import com.baseframe.sys.entity.FrameModule;
import com.baseframe.sys.service.IFrameModuleService;
import com.baseframe.sys.service.IFrameUserRoleService;
import com.baseframe.sys.service.IFrameUserService;
import com.baseframe.sys.service.dao.FrameModuleRepository;

import org.springframework.transaction.annotation.Transactional;

/**
 * @author chenjia ghoulbfo@gmail.com
 * @date 2018/4/10 11:17
 */
@Service("sysMenuService")
public class FrameModuleServiceImpl implements IFrameModuleService {


    @Autowired
    private IFrameUserService sysUserService;
    @Autowired
    private IFrameUserRoleService userRoleService;
    @Autowired
    private FrameModuleRepository sysMenuRepository;
    @Autowired
    private FrameModulePrivilegeRepository frameModulePrivilegeRepository;
    @PersistenceContext
    private EntityManager em;

    @Override
    public List<FrameModule> selectList() {
        return sysMenuRepository.findAll();
    }

    @Override
    public FrameModule selectById(String menuId) {
        return sysMenuRepository.findOne(menuId);
    }

    @Override
    public List<FrameModule> queryListParentId(String parentId, List<String> menuIdList) {
        List<FrameModule> menuList = queryListParentId(parentId);
        if (menuIdList == null) {
            return menuList;
        }

        List<FrameModule> userMenuList = new ArrayList<>();
        for (FrameModule menu : menuList) {
            if (menuIdList.contains(menu.getModuleguid())) {
                userMenuList.add(menu);
            }
        }
        return userMenuList;
    }

    @Override
    public List<FrameModule> queryListParentId(String parentId) {
        return sysMenuRepository.findByParentguid(parentId);
    }

    @Override
    public List<FrameModule> queryNotButtonList() {
        return sysMenuRepository.findByModuletypeNot(2);
    }

    @Override
    public List<FrameModule> getUserMenuList(String userId) {
        //系统管理员，拥有最高权限
        if (userRoleService.hasRight(userId,Constant.SUPER_ADMIN_ROLE)) {
            return getAllMenuList(null);
        }

        //用户菜单列表
        //后期应该从frame_module_privilege里取，2018/4/15，mzl
        //List<String> menuIdList = sysUserService.queryAllMenuId(userId);
        String hql="FROM FrameModule where moduleguid in (select moduleguid from FrameModulePrivilege where Allowtype='Role' and Allowto in (select roleguid from FrameUserRole where userguid='"+userId+"'))";
        List <FrameModule> list=em.createQuery(hql).getResultList();
        return list;
    }

    @Override
    public void delete(String menuId) {
        //删除菜单
        sysMenuRepository.delete(menuId);
        //删除菜单与角色关联
        //sysRoleMenuService.deleteByMap(new MapUtils().put("menu_id", menuId));
    }

    @Override
    public void insert(FrameModule frameModule) {
        sysMenuRepository.save(frameModule);
    }

    @Override
    public void update(FrameModule frameModule) {
        sysMenuRepository.save(frameModule);
    }

    /**
     * 获取所有菜单列表
     */
    private List<FrameModule> getAllMenuList(List<String> menuIdList) {
        //查询根菜单列表
        List<FrameModule> menuList = queryListParentId("0", menuIdList);
        //递归获取子菜单
        getMenuTreeList(menuList, menuIdList);

        return menuList;
    }

    /**
     * 递归
     */
    private List<FrameModule> getMenuTreeList(List<FrameModule> menuList, List<String> menuIdList) {
        List<FrameModule> subMenuList = new ArrayList<>();

        for (FrameModule entity : menuList) {
            //目录
            if (entity.getModuletype() == Constant.MenuType.CATALOG.getValue()) {
                entity.setList(getMenuTreeList(queryListParentId(entity.getModuleguid(), menuIdList), menuIdList));
            }
            subMenuList.add(entity);
        }

        return subMenuList;
    }

    @Override
    public List<FrameModulePrivilege> findByAllowtype(String roleType) {
        return frameModulePrivilegeRepository.findByAllowtype("Role");
    }

    @Override
    public int deleteByModuleguidAndAllowtype(String moduleGuid, String roleType) {
        return frameModulePrivilegeRepository.deleteByModuleguidAndAllowtype(moduleGuid,roleType);
    }

    @Override
    public void save(FrameModulePrivilege frameModulePrivilege) {
         frameModulePrivilegeRepository.save(frameModulePrivilege);
    }
}
